import { Context } from 'koa';

import * as argon2 from 'argon2';
import { getManager } from 'typeorm';

import { User } from '../entity/user';

import jwt from 'jsonwebtoken';

import { JWT_SECRET } from '../constants';

import { UnauthorizedException } from '../exceptions/user';

export default class AuthController {
  public static async login(ctx: Context) {
    const userRepository = getManager().getRepository(User);

    const user = await userRepository
      .createQueryBuilder()
      .where({ name: ctx.request.body.name })
      .addSelect('User.password')
      .getOne();

    if (!user) {
      throw new UnauthorizedException('用户名不存在');
    } else if (await argon2.verify(user.password, ctx.request.body.password)) {
      ctx.status = 200;
      ctx.body = { token: jwt.sign({ id: user.id }, JWT_SECRET) };
    } else {
      throw new UnauthorizedException('密码错误');
    }
  }

  public static async register(ctx: Context) {
    const userRepository = getManager().getRepository(User);

    const repeatedUser = await userRepository
      .createQueryBuilder()
      .where({ name: ctx.request.body.name })
      .getOne();

    if (repeatedUser) {
      ctx.status = 409;
      ctx.body = {
        message: 'repeated User Name',
      };
      return;
    }

    const newUser = new User();
    newUser.name = ctx.request.body.name;
    newUser.password = await argon2.hash(ctx.request.body.password);

    // 保存到数据库
    const user = await userRepository.save(newUser);

    ctx.status = 201;
    ctx.body = user;
  }
}
